package oa.hleast.xswl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.xswl.mapper.UmsAddressMapper;
import oa.hleast.xswl.pojo.domain.UmsAddress;
import oa.hleast.xswl.service.IUmsAddressService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class UmsAddressServiceImpl extends ServiceImpl<UmsAddressMapper, UmsAddress> implements IUmsAddressService {


    @Override
    public List<UmsAddress> findUserAddress(Long userId) {
        List<UmsAddress> addressList = this.list(new LambdaQueryWrapper<UmsAddress>()
                .eq(UmsAddress::getUserId, userId)
                .orderByDesc(UmsAddress::getDefaulted));
        log.info("addressList = {}", addressList);
        List<UmsAddress> returnAddressList = addressList.stream().map(pojo -> {
            if (ObjectUtils.isEmpty(pojo.getZipCode())) {
                pojo.setZipCode("");
            }
            return pojo;
        }).collect(Collectors.toList());
        log.info("returnAddressList = {}", returnAddressList);

        return returnAddressList;
    }
}
